//
//  HMPictureView.swift
//  swift微博项目
//
//  Created by huodian on 16/9/28.
//  Copyright © 2016年 huodian. All rights reserved.
//

import UIKit

let reuserCell = "HMPictureViewCell"


class HMPictureView: UICollectionView {

    var pictureInfos : [HMStatusPictureInfo]?{
        
        didSet{
            
               la.text = "\(pictureInfos?.count ?? 0)张"
            
        
            
               reloadData()
            
        }
        
     
        
    }
    
    
    override func awakeFromNib() {
        
       // self.backgroundColor = random()
        
        self.dataSource = self
        
        self.addSubview(la)
        
        la.snp.makeConstraints { (make) in
            make.center.equalTo(self)
        }
        
        
        self.register(HMPictureViewCell.self, forCellWithReuseIdentifier: reuserCell)
        
     
        
    }
    
    
    
    lazy var la : UILabel = {
        
        let lav = UILabel(title: "", font: 14, corlor: UIColor.blue)
        
       return lav
        
    }()
    
   
    
    

}

extension HMPictureView:UICollectionViewDataSource{
    
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return pictureInfos?.count ?? 0
        
    }
    
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let  cell  = collectionView.dequeueReusableCell(withReuseIdentifier: reuserCell, for: indexPath) as! HMPictureViewCell
        
        cell.pictureInfo = pictureInfos?[indexPath.item]
        
        return cell
        
    }
    
    
}
//一个文件中可以写多个类
class HMPictureViewCell: UICollectionViewCell {
    
    
    var pictureInfo : HMStatusPictureInfo?{
        
        didSet{
            
            let url = URL(string:(pictureInfo?.thumbnail_pic)!)
            
            
            pictureimageView.sd_setImage(with: url)
            
            
            //添加GIF图片的判断
            gifView.isHidden = !url!.absoluteString.hasSuffix(".gif")
            
            
        }
        
    }
    
    
    override init(frame: CGRect) {
          super.init(frame: frame)
        
        
        self.contentView.addSubview(pictureimageView)
        
        
        pictureimageView.snp.makeConstraints { (make) in
            make.edges.equalTo(self.contentView.snp.edges)
        }
        
        self.addSubview(gifView)
        
        gifView.snp.makeConstraints { (make) in
            
            make.bottom.trailing.equalTo(self)
            
        }
        
      
        
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    
    lazy var pictureimageView : UIImageView = {
        
       let le = UIImageView()
        
        //手写代码添加图片必须写以下两行代码进行图片设置
        
        le.contentMode = .scaleAspectFill
        
        le.clipsToBounds = true
        
        return le
   
    }()
    
    
    
    
    lazy var gifView : UIImageView = UIImageView(image: #imageLiteral(resourceName: "timeline_image_gif"))
    
    
    
    
    
    
}
